package com.xzgj.fmmall.service.impl;

import com.xzgj.fmmall.dao.UserAddrMapper;
import com.xzgj.fmmall.entity.ProductSku;
import com.xzgj.fmmall.entity.UserAddr;
import com.xzgj.fmmall.service.UserAddrService;
import com.xzgj.fmmall.vo.ResStatus;
import com.xzgj.fmmall.vo.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserAddrServiceImpl implements UserAddrService {
    @Resource
    private UserAddrMapper userAddrMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO listAddrByUserId(int userId) {
        Example example = new Example(UserAddr.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("user_id",userId);
        criteria.andEqualTo("status",1);

        List<UserAddr> userAddrs = userAddrMapper.selectByExample(example);
        if (userAddrs.size() == 0){
            return new ResultVO(ResStatus.NO,ResStatus.addressIsNotExistsOfUser,null);
        }else {
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,userAddrs);
        }
    }

    @Override
    public ResultVO insertAddrByUserId(@Param("UserAddr") UserAddr userAddr) {
        String addressId = new Random().nextInt(8999) + 1000 + "";

        userAddr.setAddr_id(addressId);
        userAddr.setUser_id(userAddr.getUser_id());
        userAddr.setReceiver_name(userAddr.getReceiver_name());
        userAddr.setReceiver_mobile(userAddr.getReceiver_mobile());
        userAddr.setProvince(userAddr.getProvince());
        userAddr.setCity(userAddr.getCity());
        userAddr.setArea(userAddr.getArea());
        userAddr.setAddr(userAddr.getAddr());
        userAddr.setPost_code(userAddr.getPost_code());
        userAddr.setStatus(userAddr.getStatus());
        userAddr.setCommon_addr(userAddr.getCommon_addr());
        userAddr.setCreate_time(userAddr.getCreate_time());
        userAddr.setUpdate_time(userAddr.getUpdate_time());
        if (userAddr.getUser_id() == null){
            return new ResultVO(ResStatus.NO,ResStatus.isNotExist,null);
        }
        int result = userAddrMapper.insert(userAddr);
        if (result > 0){
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,userAddr);
        }else {
            return new ResultVO(ResStatus.NO,ResStatus.FAILURE,null);

        }
    }

    @Override
    public ResultVO updateAddrByUserId(@Param("UserAddr") UserAddr userAddr) {

        //updateByPrimaryKeySelective会对实体类字段进行判断再更新(如果为Null就忽略更新)，如果字段为空，忽略不更新        userAddr.setAddr_id(userAddr.getAddr_id());
        int result = userAddrMapper.updateUserAddrInfoByUserId(userAddr,userAddr.getUser_id());
        if (result > 0){
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,userAddr);
        }else {
            return new ResultVO(ResStatus.NO,ResStatus.addressIsNotExistsOfUser,null);

        }
    }

    @Override
    public ResultVO deleteAddrByAddrId(String addrId) {
//        Example example = new Example(UserAddr.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("addr_id",addrId);
//        criteria.andEqualTo("status",1);
//        int result = userAddrMapper.deleteByPrimaryKey(example);
        int result = userAddrMapper.deleteAddrByAddrId(addrId);

        if (result > 0){
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
        }else {
            return new ResultVO(ResStatus.NO,ResStatus.FAILURE,null);

        }
    }

    @Override
    public ResultVO setDefault(Integer aid, Integer uid, String username) {
        UserAddr result = userAddrMapper.findByAid(aid);
        if (result == null){
            return new ResultVO(ResStatus.NO,ResStatus.userAddrIsNotExist,null);
        }
        //检测当前获取到的收货地址数据的归属
        if (!result.getUser_id().equals(uid)){
            return new ResultVO(ResStatus.NO,ResStatus.illegalUserAddrIsNotExist,null);
        }
        //先将所哟丶收货地址设置非默认
        Integer rows = userAddrMapper.updateNonDefault(uid);
        if (rows < 1){
            return new ResultVO(ResStatus.NO,ResStatus.illegalUserAddrUpdateOperation,null);
        }
        //将用户选中的某条地址设置为默认地址
        rows = userAddrMapper.updateDefaultByAid(aid,username,new Date());
        if (rows < 1){
            return new ResultVO(ResStatus.NO,ResStatus.illegalUserAddrUpdateOperation,null);
        }

        return new ResultVO(ResStatus.OK,ResStatus.operationSuccess,null);

    }


}
